package com.chamberlain.equipment.service.impl;

import com.chamberlain.common.core.domain.entity.Equipment;
import com.chamberlain.common.core.domain.entity.Scene;
import com.chamberlain.common.core.domain.entity.SysUser;
import com.chamberlain.common.core.domain.model.LoginUser;
import com.chamberlain.common.utils.DateUtils;
import com.chamberlain.equipment.domain.UserScene;
import com.chamberlain.equipment.mapper.EquipmentMapper;
import com.chamberlain.equipment.mapper.SceneEquipmentMapper;
import com.chamberlain.equipment.mapper.SceneMapper;
import com.chamberlain.equipment.mapper.UserSceneMapper;
import com.chamberlain.equipment.service.ISceneService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: CYY
 * @date: 2024/3/25
 */
@Service
public class SceneServiceImpl implements ISceneService {
    @Resource
    private SceneMapper sceneMapper;

    @Resource
    private UserSceneMapper userSceneMapper;

    @Resource
    private EquipmentMapper equipmentMapper;

    @Resource
    private SceneEquipmentMapper sceneEquipmentMapper;

    /**
     * 查询场景
     *
     * @param id 场景主键
     * @return 场景
     */
    @Override
    public Scene selectSceneById(Long id)
    {
        return sceneMapper.selectSceneById(id);
    }

    /**
     * 查询场景列表
     *
     * @param scene 场景
     * @return 场景
     */
    @Override
    public List<Scene> selectSceneList(Scene scene)
    {
        return sceneMapper.selectSceneList(scene);
    }

    /**
     * 新增场景
     *
     * @param scene 场景
     * @return 结果
     */
    @Override
    @Transactional
    public int insertScene(Scene scene, LoginUser loginUser)
    {
        scene.setCreatedTime(DateUtils.getNowDate());
        scene.setCreatedBy(loginUser.getUsername());
        sceneMapper.insertScene(scene);

        UserScene userScene =new UserScene();
        userScene.setSceneId(scene.getId());
        userScene.setUserId(loginUser.getUserId());
        userScene.setCreatedBy(loginUser.getUsername());
        userScene.setCreatedTime(DateUtils.getNowDate());
        return userSceneMapper.insert(userScene);
    }

    /**
     * 修改场景
     *
     * @param scene 场景
     * @return 结果
     */
    @Override
    public int updateScene(Scene scene)
    {
        return sceneMapper.updateScene(scene);
    }

    /**
     * 批量删除场景
     *
     * @param ids    需要删除的场景主键
     * @param userId
     * @return 结果
     */
    @Override
    public int deleteSceneByIds(Long[] ids, Long userId)
    {
        SysUser sysUser=new SysUser();
        sysUser.setUserId(userId);
        sysUser.setSceneIds(ids);
        sceneMapper.deleteSceneByIds(ids);
        return userSceneMapper.deleteByUserAndSceneId(sysUser.getUserId(),sysUser.getSceneIds());
    }

    /**
     * 删除场景信息
     *
     * @param id 场景主键
     * @return 结果
     */
    @Override
    public int deleteSceneById(Long id)
    {
        return sceneMapper.deleteSceneById(id);
    }

    @Override
    public List<Scene> selectSceneListByUser(SysUser sysUser) {
        List<UserScene> userSceneList= userSceneMapper.queryByUserId(sysUser.getUserId());
        Long[] scenIds= new Long[userSceneList.size()];
        for (int i = 0; i < userSceneList.size(); i++) {
            scenIds[i] = userSceneList.get(i).getSceneId();
        }
        List<Scene> sceneList = new LinkedList<>();
        for (int i = 0; i < scenIds.length; i++) {
            Scene scene = sceneMapper.selectSceneById(scenIds[i]);
            List<Equipment> equipmentList =new ArrayList<>();
            Long[] equipmentIds = sceneEquipmentMapper.queryAllBySceneId(scene.getId());
            for (int j = 0; j < equipmentIds.length; j++) {
                equipmentList.add(equipmentMapper.selectEquipmentById(equipmentIds[j]));
            }
            scene.setEquipments(equipmentList);
            sceneList.add(scene);
        }
        return sceneList;
    }
}
